﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace SmartProject.Website
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (UI.BasePage.IsFirstTimePreferenceSetup ||
                    UI.BasePage.IsFirstTimeUserSetup)
                {
                    FormsAuthentication.RedirectFromLoginPage(
                        SmartProject.BusinessObjects.User.CreateTemporaryUser(), true);
                }
                else
                {
                    if (User.Identity.IsAuthenticated)
                    {
                        FormsAuthentication.RedirectFromLoginPage(User.Identity.Name, true);
                    }
                }
            }
        }

        protected void BtnLogin_Click(object sender, EventArgs e)
        {
            this.LblLoginError.Text = String.Empty;
            string userName = this.TxtUsername.Text;
            string password = this.TxtPassword.Text;

            if (!String.IsNullOrEmpty(userName) && !String.IsNullOrEmpty(password))
            {
                if (Membership.ValidateUser(userName, password))
                {
                    FormsAuthentication.RedirectFromLoginPage(userName, true);
                }
                else
                {
                    Logging.Logger.Log("Default.aspx", "BtnLogin_Click", "Login failed", "Debug");
                    this.LblLoginError.Text = "The username or password you supplied is incorrect.";
                }
            }
        }
    }
}
